namespace Voodoo.Sauce.Internal.Ads.MopubMediation
{
	public class MopubRewardedVideo : RewardedVideoAd
	{
		public MopubRewardedVideo(string adUnit, bool autofetch)
			: base("Mopub", adUnit, autofetch)
		{
			MoPubWrapper.LoadRewardedVideoPluginsForAdUnits(adUnit);
			RegisterEvents();
			AutoFetch();
		}

		protected override void Fetch()
		{
			if (!IsFetching && !IsLoaded())
			{
				base.Fetch();
				MoPubAndroid.RequestRewardedVideo(_adUnit);
			}
		}

		public override void Show()
		{
			base.Show();
			MoPubAndroid.ShowRewardedVideo(_adUnit);
		}

		public override bool IsLoaded()
		{
			return MoPubAndroid.HasRewardedVideo(_adUnit);
		}

		protected override void RegisterEvents()
		{
			base.RegisterEvents();
			MoPubManager.OnRewardedVideoLoadedEvent += OnMoPubLoadedEvent;
			MoPubManager.OnRewardedVideoFailedEvent += OnMoPubFailedEvent;
			MoPubManager.OnRewardedVideoFailedToPlayEvent += OnMoPubFailedToPlayEvent;
			MoPubManager.OnRewardedVideoExpiredEvent += OnMoPubExpiredEvent;
			MoPubManager.OnRewardedVideoShownEvent += OnMoPubShownEvent;
			MoPubManager.OnRewardedVideoClickedEvent += OnMoPubClickedEvent;
			MoPubManager.OnRewardedVideoClosedEvent += OnMoPubClosedEvent;
			MoPubManager.OnRewardedVideoLeavingApplicationEvent += OnMoPubLeavingApplicationEvent;
			MoPubManager.OnRewardedVideoReceivedRewardEvent += OnMoPubReceivedRewardEvent;
		}

		protected override void UnregisterEvents()
		{
			base.UnregisterEvents();
			MoPubManager.OnRewardedVideoLoadedEvent -= OnMoPubLoadedEvent;
			MoPubManager.OnRewardedVideoFailedEvent -= OnMoPubFailedEvent;
			MoPubManager.OnRewardedVideoFailedToPlayEvent -= OnMoPubFailedToPlayEvent;
			MoPubManager.OnRewardedVideoExpiredEvent -= OnMoPubExpiredEvent;
			MoPubManager.OnRewardedVideoShownEvent -= OnMoPubShownEvent;
			MoPubManager.OnRewardedVideoClickedEvent -= OnMoPubClickedEvent;
			MoPubManager.OnRewardedVideoClosedEvent -= OnMoPubClosedEvent;
			MoPubManager.OnRewardedVideoLeavingApplicationEvent -= OnMoPubLeavingApplicationEvent;
			MoPubManager.OnRewardedVideoReceivedRewardEvent -= OnMoPubReceivedRewardEvent;
		}

		private void OnMoPubLoadedEvent(string adUnit)
		{
			if (!(adUnit != _adUnit))
			{
				OnAdLoadedCallback();
			}
		}

		private void OnMoPubFailedEvent(string adUnit, string errorMsg)
		{
			if (!(adUnit != _adUnit))
			{
				OnAdFailedCallback(errorMsg);
			}
		}

		private void OnMoPubFailedToPlayEvent(string adUnit, string errorMsg)
		{
			if (!(adUnit != _adUnit))
			{
				OnAdFailedToPlayCallback(errorMsg);
			}
		}

		private void OnMoPubExpiredEvent(string adUnit)
		{
			if (!(adUnit != _adUnit))
			{
				OnAdExpiredCallback();
			}
		}

		private void OnMoPubReceivedRewardEvent(string adunit, string reward, float amount)
		{
			if (!(adunit != _adUnit))
			{
				OnAdReceivedRewardCallback();
			}
		}

		private void OnMoPubClosedEvent(string adUnit)
		{
			if (!(adUnit != _adUnit))
			{
				OnAdClosedCallback();
			}
		}

		private void OnMoPubShownEvent(string adUnit)
		{
			if (!(adUnit != _adUnit))
			{
				DebugLog("OnMoPubShownEvent");
			}
		}

		private void OnMoPubClickedEvent(string adUnit)
		{
			if (!(adUnit != _adUnit))
			{
				DebugLog("OnMoPubClickedEvent");
			}
		}

		private void OnMoPubLeavingApplicationEvent(string adUnit)
		{
			if (!(adUnit != _adUnit))
			{
				DebugLog("OnMoPubLeavingApplicationEvent");
			}
		}
	}
}
